<?php

global $CFG;
require_once( $CFG->dirroot . 'mod/folio/model/resource.php');

class folio_ResourceTree extends folio_BaseView {

    private $page;
    private $user;

    function __construct( folio_page $page, folio_user $user ) {
        $url = url;
        $p = 'folio_tree';
        global $CFG;

        $this->page = $page;
        $this->user = $user;

        $this->html = <<< END
<div id='{$p}clear' class='{$p}clear'>
    <div id='{$p}tree' class="yui-skin-sam">
        <div id="{$p}treeDiv1"></div>
        <img src="{$url}mod/folio/image/tree_blankimage.jpg" onload="javascript:{$p}treeInit()"  />
    </div>
</div>
END;

        // Load Resources
        $key = folio_resource::encodeKey(-1, 'blog', $this->page->user_ident);
        $blogs = folio_resource::SelectWhereKey($key);
        $key = folio_resource::encodeKey(-1, 'file', $this->page->user_ident);
        $files = folio_resource::SelectWhereKey($key );
        $key = folio_resource::encodeKey(-1, 'image', $this->page->user_ident);
        $images = folio_resource::SelectWhereKey($key );

        // Load into tree nodes.
        $url = url;
        $p = 'folio_tree';

        // Build javascript to load parent information.
        $buildNodes =
            folio_resourcetree::getNodes( 'blog', 'Blog&nbsp;Posts', $blogs, $this->page, $this->user ) .
            folio_resourcetree::getNodes( 'file', 'Files', $files, $this->page, $this->user ) .
            folio_resourcetree::getNodes( 'image', 'Images', $images, $this->page, $this->user );

        // Build the text body to go in the sidebar.
        $this->js = <<< END
var {$p}tree, {$p}currentIconMode;

function {$p}treeInit() {
    {$p}currentIconMode=1;
    tree = new YAHOO.widget.TreeView("{$p}treeDiv1");
    //tree.setDynamicLoad({$p}loadDataForNode); //, {$p}currentIconMode);

    var root = tree.getRoot();

    {$buildNodes}

    tree.draw();
}
END;
        $this->metatags = array (
            'mod/folio/css/treeview.css',
            'mod/folio/lib/yui_2.4.1/build/yahoo/yahoo-min.js',
            'mod/folio/lib/yui_2.4.1/build/event/event-min.js',
            'mod/folio/lib/yui_2.4.1/build/connection/connection-min.js',
            'mod/folio/lib/yui_2.4.1/build/treeview/treeview-min.js',
            'mod/folio/js/treeview.js');

    }

    /**
     * Return tree nodes.
     *
     * @param resources[] $resources
     * @param folio_page $page
     * @param folio_user $user
     * @return html
     */
    private function getNodes( $type, $title, $resources, $page, $user ) {
        $p = 'folio_resource';

        // Create empty type node.
        $s = <<< END
var {$p}myObj{$type} = {label: "{$title}", id:"{$type}" };
var {$p}Node{$type} = new YAHOO.widget.TextNode({$p}myObj{$type}, root, false);
  {$p}Node{$type}.iconMode  = 1;
END;

        if( $resources) {
            foreach ($resources as $r) {
                $title = folio_resourcetree::folio_tree_formattitle($r->title);
                $key = folio_resource::encodeKey($r);
                $i = str_replace(':','', $key);

                $s .= <<< END
var {$p}myObj{$i} = {label: "{$title}", id:"{$key}" };
var {$p}Node{$i} = new YAHOO.widget.TextNode({$p}myObj{$i}, {$p}Node{$type}, false);
  {$p}Node{$i}.iconMode  = 1;
  {$p}Node{$i}.href = "javascript:folio_RichEditor_AddResource('{$key}', {$page->page_ident});";
END;
            }
        }
        return $s;
    }


    /**
    * This function formats the title of a page in a suitable format for the treeview control
    *   As of Feb 2007, v.7 of Elgg forces a td.width declaration, which messes up the  yahoo treeview control
    *   As a fix, I do a duplicate declaration to set td.width=10px.  However, this means that the title needs to be
    *   forced to have the correct length.  So, I use non-breaking spaces to fill it out.  However, allowing some breaking
    *   spaces is a good idea, as otherwise it gets too long. So separate into chunks
    *
    * @param string $title Incoming title
    * @return string
    **/
    function folio_tree_formattitle( $title ) {
        // max len
        //return $title;
        $len = 25;
        $temp = '';

        $title = str_replace("\"", "'", trim($title) );
        $parts = explode(' ', $title);
        $title = '';

        foreach ( $parts as $part ) {
            if ( strlen( $temp ) == 0 ) {
                // Nothing in temp variable
                $temp = $part;
            } elseif ( ( strlen($temp) + strlen($part) ) < $len ) {
                // Can join
                $temp = $temp . '~' . $part;
            } else {
                // Too long.  Make new temp & move old temp to return value.
                $title = ltrim( $title . ' ' . $temp );
                $temp = $part;
            }
        }
        // Move last temp to title
        $title = str_replace( '~', '&nbsp;', trim( $title . ' ' . $temp ) );
        return $title;
    }

    // Call to return a resource list with the passed resources.
    public function ResourceList( $resources, $page_owner, $page_ident ) {
        $prefix = 'folio_resource';
        $html = '';

        if ( !$resources ) return '';
        if ( !is_array($resources )) { return $resources; }

        foreach( $resources as $a ) {

            $key = folio_resource::encodeKey( $a );

            $html .= <<< END
            <li id="{$prefix}{$a->item_ident}">
                <span onclick="javascript:folio_RichEditor_AddResource('{$key}', {$page_ident});" >{$a->title}</span>
            </li>
END;
        }

        return $html;
    }
}
?>
